<?php
/**
 * 对象存储服务
 * @文件名称: DmpOss.php
 * @author: jawei
 * @Email: gaozhiwei@etcp.cn
 * @Date: 2017-06-06
 * @Copyright: 2017 悦畅科技有限公司. All rights reserved.
 * 注意：本内容仅限于悦畅科技有限公司内部传阅，禁止外泄以及用于其他的商业目的
 */
namespace app\components;
use app\base\BaseException;
use Yii;
class DmpOss
{
    public static function usageDemo()
    {
        $uploadFile = \Yii::$app->params['projectRoot'] . '/upload/upload_user.txt';
        $object = DmpOss::getObjectNameByFileUrl($uploadFile);

        $instance = DmpOss::getInstance();

        $res = DmpOss::putObject($instance, $uploadFile, $object);
        var_dump($res); // bool true

        $savePath = \Yii::$app->params['projectRoot'] . '/upload/download_user.txt';
        $res = DmpOss::getObject($instance, $object, $savePath);
        var_dump($res); // bool true
    }

    /**
     * 获取操作实例
     *
     * @return \OssUtil
     */
    public static function getInstance()
    {
        require_once dirname(__DIR__) . '/services/jdb/qc-php-sdk/OssUtil.php';

        return new \OssUtil(
            Yii::$app->params['weed-obs']['appKeyId'],
            Yii::$app->params['weed-obs']['appKeySecret'],
            Yii::$app->params['weed-obs']['host']);
    }

    /**
     * 存储一个对象
     *
     * @param \OssUtil $instance
     * @param $localPath string 要上传文件的本地绝对路径
     * @param $objectName string 存储的对象路径
     * @param $objectName
     * @return mixed
     * @throws BaseException
     */
    public static function putObject(\OssUtil $instance, $localPath, $objectName)
    {
        if (!file_exists($localPath) || filesize($localPath) == 0) {
            throw new BaseException('文件不存在或文件为空', 4036150);
        }

        $result = $instance->put_object(Yii::$app->params['weed-obs']['bucket'], $objectName, $localPath);
        return $result;
    }

    /**
     * 获得一个对象
     *
     * @param \OssUtil $instance
     * @param $objectName string 存储在OBS上的对象路径 Eg: test/test.jpeg
     * @param $savePath string 存储在本地的绝对路径
     * @return \ResponseCore
     */
    public static function getObject(\OssUtil $instance, $objectName, $savePath)
    {
        return $instance->get_object(Yii::$app->params['weed-obs']['bucket'], $objectName, $savePath);
    }

    /**
     * 通过文件存储的url获得上传的对象路径
     *
     * @param $url
     * @return string
     */
    public static function getObjectNameByFileUrl($url)
    {
        return Yii::$app->params['weed-obs']['upload-prefix']
        . '/' . date('YmdHi')
        . '/' . substr(md5(basename($url)), 0, 6)
        . '.' . substr($url, strrpos($url, '.') + 1);
    }

    /**
     * 获取对象并存储在本地, 返回临时存储文件地址.
     *
     * @param $objName
     * @return string
     * @throws BaseException
     */
    public static function getAndSaveLocal($objName)
    {
        $type = substr($objName, strrpos($objName, '.') + 1);
        $localPath = '/tmp/' . substr(md5($objName), 0, 6) . $type;
        $result = self::getObject(self::getInstance(), $objName, $localPath);
        if(!$result){
            throw new BaseException('获取对象存储数据失败.', 50010258);
        }

        return $localPath;
    }

    public static function getLocalPathFileName($localPath)
    {
        return substr(md5($localPath), 0, 6) . substr($localPath, strrpos($localPath, '.') + 1);
    }
}
